package com.demo;


import com.dao.fruitMapper;
import com.demo.fruit;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;

public class Test01 {


    public static void main(String[] args) throws IOException {
        String resource = "SqlMapConfig.xml";           //绑定主体.xml

        //对SqlMapConfig.xml读取并解析
        InputStream inputStream = Resources.getResourceAsStream(resource);

        //获取会话工厂SqlSessionFactory
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        //SqlSession主要用于对数据库的操作
        SqlSession session =  sessionFactory.openSession();     //获取数据库执行对象

        fruitMapper mapper = session.getMapper(fruitMapper.class);    //接口名.class 获取对象   反射机制，通过接口，执行.xml里sql语句


//        haspmap 创建
        HashMap<String, Object> hashMap = new HashMap<>();
//        传入参数
          hashMap.put("name","香蕉");
//        hashMap.put("id","90001");
//        hashMap.put("num",1);

//       有多个查询结果时候返回值用集合接受并for 取结果
        ArrayList <fruit> fruitlist = mapper.getFruit(hashMap);


        //fruit fruitById = mapper.getFruitById("90001");//查询   方法名和.xml 中的id相同
        //取出结果
        for(fruit fruitById : fruitlist){
            System.out.println("名字："+fruitById.getName());
            System.out.println("ID："+fruitById.getId());
            System.out.println("进价："+fruitById.getInprice());
            System.out.println("售价："+fruitById.getOutprice());
            System.out.println("数量："+fruitById.getNum());
            System.out.println("单位："+fruitById.getUnit());
        }




        session.commit();     //增删改需要执行事件 查询不用
        session.close();      //关闭
        inputStream.close();
    }
    }


